// 以下为官方示例
$().ready(function() {
	validateRule();
	// $("#signupForm").validate();	

});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});

//



function update() {
	$("#roleIds").val(getCheckedRoles());

	$.ajax({
		cache : true,
		type : "POST",
		url : "/sys/user/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg(data.msg);
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.msg(data.msg);
			}

		}
	});

}
function getCheckedRoles() {
	var adIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}
function setCheckedRoles() {
	var roleIds = $("#roleIds").val();
	alert(roleIds);
	var adIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}


//手机号码验证身份证正则合并：(^\d{15}$)|(^\d{17}([0-9]|X)$)
jQuery.validator.addMethod("isPhone",function(value,element){
	var length = value.length;
	var phone=/^1[3|4|5|7|8][0-9]\d{8}$/;
	return this.optional(element)||(length == 11 && phone.test(value));
},"请填写正确的11位手机号");

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			username : {
				required : true,
				minlength : 2
			},
			password : {
				required : true,
				minlength : 6
			},
			confirm_password : {
				required : true,
				minlength : 6,
				equalTo : "#password"
			},
			email : {
				required : true,
				email : true
			},
			mobile : {
				required : true,
				isPhone: true,
				maxlength :  100
			},
			topic : {
				required : "#newsletter:checked",
				minlength : 2
			},
			numberkbn : {
				maxlength:11,
				number: true
			},
			agree : "required"
		},
		messages : {

			name : {
				required : icon + "请输入姓名"
			},
			username : {
				required : icon + "请输入您的用户名",
				minlength : icon + "用户名必须两个字符以上"
			},
			password : {
				required : icon + "请输入您的密码",
				minlength : icon + "密码必须6个字符以上"
			},
			confirm_password : {
				required : icon + "请再次输入密码",
				minlength : icon + "密码必须6个字符以上",
				equalTo : icon + "两次输入的密码不一致"
			},
			numberkbn : {
				maxlength : icon + "输入最大的位数是11位",
				number : icon + "请输入有效的数字"
			},
			email : icon + "请输入您的E-mail"
		}
	})
}
var openDept = function(){
	layer.open({
		type:2,
		title:"选择部门",
		area : [ '300px', '450px' ],
		content:"/system/sysDept/treeView"
	})
}
function loadDept( deptId,deptName){
	$("#deptId").val(deptId);
	$("#deptName").val(deptName);
}




$('input[type=radio][name=customsDeptFlg]').change(function() {
    if (this.value == '0') {
    		$.ajax({
    			cache : true,
    			type : "GET",
    			url : "/taxreturns/customsArea/listAll",
    			async : false,
    			error : function(request) {
    				parent.layer.alert("Connection error");
    			},
    			success : function(data) {
    				$("#customsDeptCode").empty();
    				if (data.code == 0) {
    					for(var i=0;i<data.codeList.length;i++){
    						   $("#customsDeptCode").append("<option value='" + data.codeList[i].customsCode + "'>" + data.codeList[i].customsName + "</option>");
    					}
    				}
    			}
    		});
     }
//     else if (this.value == '1') {
//    			$.ajax({
//    				cache : true,
//    				type : "GET",
//    				url : "/taxreturns/shippingAgencyInfo/listAll",
//    				async : false,
//    				error : function(request) {
//    					parent.layer.alert("Connection error");
//    				},
//    				success : function(data) {
//    					$("#customsDeptCode").empty();
//    					if (data.code == 0) {
//    						for(var i=0;i<data.codeList.length;i++){
//    							  $("#customsDeptCode").append("<option value='" + data.codeList[i].tradeCode + "'>" + data.codeList[i].tradeName + "</option>");
//        				    }
//    					}
//    				}
//    			});
//     	}
});




var type=  $('input[name="customsDeptFlg"]:checked').val(); 
 //if(type =='0'){
	 $.ajax({
			cache : true,
			type : "GET",
			url : "/taxreturns/customsArea/listAll",
			async : false,
			error : function(request) {
				parent.layer.alert("Connection error");
			},
			success : function(data) {
				var hiddenCustomsDeptCode = $("#hiddenCustomsDeptCode").val();
				$("#customsDeptCode").empty();
				if (data.code == 0) {
					for(var i=0;i<data.codeList.length;i++){
						 if(data.codeList[i].customsCode==hiddenCustomsDeptCode){
							  $("#customsDeptCode").append("<option value='" + data.codeList[i].customsCode + "' selected='selected'>" + data.codeList[i].customsName + "</option>");
						  }else{
							  $("#customsDeptCode").append("<option value='" + data.codeList[i].customsCode + "'>" + data.codeList[i].customsName + "</option>");
						  }
					}
				}
			}
		});
 //}
 
// if(type =='1'){
//	 $.ajax({
//			cache : true,
//			type : "GET",
//			url : "/taxreturns/shippingAgencyInfo/listAll",
//			async : false,
//			error : function(request) {
//				parent.layer.alert("Connection error");
//			},
//			success : function(data) {
//				debugger
//				var hiddenCustomsDeptCode = $("#hiddenCustomsDeptCode").val();
//				$("#customsDeptCode").empty();
//				if (data.code == 0) {
//					for(var i=0;i<data.codeList.length;i++){
//						 if(data.codeList[i].tradeCode==hiddenCustomsDeptCode){
//							  $("#customsDeptCode").append("<option value='" + data.codeList[i].tradeCode + "' selected='selected'>" + data.codeList[i].tradeName + "</option>");
//						  }else{
//							  $("#customsDeptCode").append("<option value='" + data.codeList[i].tradeCode + "'>" + data.codeList[i].tradeName + "</option>");
//						  }
//					}
//				}
//			}
//		});
// }